/**
 * 幻灯片演示系统
 * @description 提供幻灯片切换、键盘导航和侧边栏功能
 */
document.addEventListener('DOMContentLoaded', function() {
    // 获取所有幻灯片和相关元素
    const slides = document.querySelectorAll('.slide');
    const prevBtn = document.getElementById('prevBtn');
    const nextBtn = document.getElementById('nextBtn');
    const slideCounter = document.getElementById('slideCounter');
    const sidebar = document.getElementById('sidebar');
    const sidebarToggle = document.getElementById('sidebarToggle');
    const slideNav = document.getElementById('slideNav');
    
    let currentSlideIndex = 0;
    const totalSlides = slides.length;
    
    /**
     * 显示指定索引的幻灯片
     * @param {number} index - 要显示的幻灯片索引
     */
    function showSlide(index) {
        // 确保索引在有效范围内
        if (index < 0) index = 0;
        if (index >= totalSlides) index = totalSlides - 1;
        
        // 隐藏所有幻灯片
        slides.forEach(slide => {
            slide.classList.remove('active');
        });
        
        // 显示当前幻灯片
        slides[index].classList.add('active');
        
        // 更新计数器
        slideCounter.textContent = `${index + 1} / ${totalSlides}`;
        
        // 更新导航菜单高亮
        const navItems = document.querySelectorAll('#slideNav li');
        navItems.forEach((item, i) => {
            if (i === index) {
                item.classList.add('active');
            } else {
                item.classList.remove('active');
            }
        });
        
        currentSlideIndex = index;
    }
    
    /**
     * 生成导航菜单
     */
    function generateNavigation() {
        slides.forEach((slide, index) => {
            const title = slide.querySelector('h1, h2').textContent;
            const li = document.createElement('li');
            li.textContent = `${index + 1}. ${title}`;
            
            li.addEventListener('click', () => {
                showSlide(index);
            });
            
            slideNav.appendChild(li);
        });
    }
    
    // 初始化导航和显示第一张幻灯片
    generateNavigation();
    showSlide(0);
    
    // 按钮事件监听
    prevBtn.addEventListener('click', () => {
        showSlide(currentSlideIndex - 1);
    });
    
    nextBtn.addEventListener('click', () => {
        showSlide(currentSlideIndex + 1);
    });
    
    // 键盘事件监听
    document.addEventListener('keydown', (e) => {
        switch(e.key) {
            case 'ArrowLeft':
                showSlide(currentSlideIndex - 1);
                break;
            case 'ArrowRight':
            case ' ':  // 空格键
                showSlide(currentSlideIndex + 1);
                break;
            case 'Home':
                showSlide(0);
                break;
            case 'End':
                showSlide(totalSlides - 1);
                break;
        }
    });
    
    // 侧边栏切换
    sidebarToggle.addEventListener('click', () => {
        sidebar.classList.toggle('open');
    });
    
    // 点击幻灯片区域关闭侧边栏
    document.querySelector('.slides-container').addEventListener('click', () => {
        sidebar.classList.remove('open');
    });
});